<?php

/** 
 * 转账记录
 * @property Water_price_mdl $price
 */
class Transfers extends Admin_Controller {
	
	/**
	 */
	public function __construct() {
		parent::__construct ();
		$this->load->model('WithdrawOrderModel','withdraw');
		$this->load->model('User_mdl','user');
	}
	
	/**
	 * 机器
	 */
	public function add(){
		//接收上传参数
		$post = $this->input->post(null,true);
		
		if ( empty($post) ){
			$this->showView('admin/transfers/add');
		}else{
			//-------处理上传表单
			$this->load->library('form_validation');
			$this->form_validation->set_rules('amount','转账金额','required|numeric');
			$this->form_validation->set_rules('wxopenid','微信openid','required');
			
			$res = $this->form_validation->run();
			
			if ( $res ){
				
				//机器信息
				$data_mechine = array(
						'user_id' => $post['user_id'],
						'amount' => $post['amount']*100,
						'desc' => $post['desc'],
						'wx_openid' => $post['wxopenid'],
						'remark'=> $post['remark'],
						'create_time' => time()
				);
				
				$resMachine = $this->withdraw->insert($data_mechine);
				
				if ($resMachine){
					success('hs_mng/transfers/get_list', '添加成功');
				}else{
					error('添加失败');
				}
			} else {
				//验证失败
				$msg=validation_errors();
				error($msg);
			}
	
		}
	
	}
	
	/**
	 * 转账列表
	 */
	public function get_list(){
		
		$page = check_page();
		$where = array();
		$like = array();
		$get = $this->input->get();
		if ( !empty($get) ){
			foreach ( $get  as $k=>$v){
				if ( $v == '' ){
					continue;
				}
				switch ($k){
					case 'id':
						$where['id'] = (int)$v;
						break;
					case 'status':
						if ( $v !=-1 ){
							$where['status'] = (int)$v;
						}
						break;
				}
			}
		}
		
		$count = $this->withdraw->count($where,$like);
		
		$list = $this->withdraw->getList($select='*',$where,$page['size'],($page['page']-1)*$page['size'],'id','DESC',$group='',$like);
		
		if ( !empty($list) ){
			$userIdList = array_column($list, 'user_id');
			$userIdlist = array_unique($userIdList);
			$userList = $this->user->getUserWhereIn($userIdlist);
			foreach ($list as $k=>$v){
				foreach ($userList as $kk=>$vv){
					if ($v['user_id'] == $vv['id']){
						$list[$k]['nickname'] = $vv['nickname'];
					}
				}
				
			}
		}
		
		$pagestring = pagestring($count, $page);
		
		$temp['list'] = $list;
		$temp['page'] = $pagestring;
		
		$this->showView('admin/transfers/get_list',$temp);
		
	}
	
	/**
	 * 选择产品
	 */
	public function chose_user(){
	
		$this->load->model('User_mdl','user');
		$page = check_page();
		$like = array();
		$get = $this->input->get();
		$where = array();
	
		if ( !empty($get) ){
			foreach ( $get  as $k=>$v){
				if ( empty($v) ){
					continue;
				}
				switch ($k){
					case 'uid': $where['id'] = (int)$get['uid'];
					break;
					case 'phone': $where['phone'] = $get['phone'];
					break;
				}
			}
		}
		$count = $this->user->count($where,$like);
		$list = $this->user->getList($select='*',$where,$page['size'],($page['page']-1)*$page['size'],'id','desc',$group='',$like);
		$pagestring = pagestring($count, $page);
		$temp['list'] = $list;
		$temp['page'] = $pagestring;
		$this->showView('admin/transfers/chose_user',$temp);
	
	}
	
	
}

